#ifndef RECORD_H
#define RECORD_H

#include "inc.h"

#define MAX_BORROW_TIME 3   /* monthos */
#define MAX_NAME_LEN    32
#define MAX_EMAIL_LEN   32
#define MAX_TITLE_LEN   32
#define RECORD_LEN(record) \
    (((record)->table == BORROW) ? (sizeof(uint16_t) * 3 + sizeof(date_t) * 2 + sizeof(uint8_t)) : \
    ((record)->table == BOOK) ? (sizeof(uint16_t) * 3 + sizeof(uint8_t) + strlen((record)->val_book.title) + strlen((record)->val_book.author) + 2) : \
    ((record)->table == USER) ? (sizeof(uint16_t) + strlen((record)->val_user.name) + strlen((record)->val_user.email) + 2) : 0)

enum BorrowStatus {
    RETURNED,
    NON_RETURNED,
    OVERDUE
};

enum BookCategory {
    FICTION,
    SCIENCE,
    HISTORY
};

enum TableType {
    BORROW,
    USER,
    BOOK
};

typedef struct Date {
    uint16_t year;
    uint8_t month;
    uint8_t day;
} date_t;

typedef struct BorrowRecord {
    uint16_t borrow_id;
    uint16_t book_id;
    uint16_t user_id;
    date_t borrow_date;
    date_t return_date;
    uint8_t status;
} borrow_t;

typedef struct UserRecord {
    uint16_t user_id;
    char name[MAX_NAME_LEN];
    char email[MAX_EMAIL_LEN];
} user_t;

typedef struct BookRecord {
    uint16_t book_id;
    uint16_t quantity;
    uint16_t available;
    uint8_t category;
    char title[MAX_TITLE_LEN];
    char author[MAX_NAME_LEN];
} book_t;

typedef struct Record {
    uint8_t table;
    uint16_t index;
    union {
        borrow_t val_borrow;
        user_t val_user;
        book_t val_book;
    };
} record_t;

#endif  /* RECORD_H */